//
//  BTELanguageManager.swift
//  BTE
//
//  Created by Mike Zhang on 2025/8/5.
//


import Foundation

// 语言类型枚举
enum AppLanguage: String {
    
    case YINDU = "YINGDU"
    case YINNI = "YINNI"
    
    static func typeFrom(code: Int) -> AppLanguage {
        return code == 1 ? .YINDU : .YINNI
    }
}

class BTELanguageManager {
    
    static let shared = BTELanguageManager()
    
    // 当前语言（默认英语）
    private(set) var currentLanguage: AppLanguage = .YINDU
    
    // 语言变更通知
    static let languageChangedNotification = Notification.Name("AreaChanged")
    
    private init() {
        loadSavedLanguage()
    }
    
    // 配置初始语言（App启动时调用）
    // BTELanguageManager.shared.configure(languageCode: serverLanguageCode)
    func configure(languageCode: Int = 1) {
        let newLanguage = AppLanguage.typeFrom(code: languageCode)
        guard newLanguage != currentLanguage else { return }
        
        currentLanguage = newLanguage
        saveLanguage()
        postNotification()
    }
    
    func localizedImageString(forKey key: String) -> String {
        return localizedImagStrings[key]?[currentLanguage] ?? key
    }
    
    func localizedString(forKey key: String) -> String {
        return localizedStrings[key]?[currentLanguage] ?? key
    }
    
    private func saveLanguage() {
        UserDefaults.standard.set(currentLanguage.rawValue, forKey: "Language")
    }
    
    private func loadSavedLanguage() {
        if let langCode = UserDefaults.standard.string(forKey: "Language"),
           let language = AppLanguage(rawValue: langCode) {
            currentLanguage = language
        }
    }
    
    private func postNotification() {
        NotificationCenter.default.post(name: BTELanguageManager.languageChangedNotification, object: nil)
    }
    
    // MARK: - Private
    fileprivate let localizedImagStrings: [String: [AppLanguage: String]] = [
        "Group 1171276709":[
            .YINDU:"Group 1171276709",
            .YINNI:"Group 11712767091"
        ],"Group 1312316871":[
            .YINDU:"Group 1312316871",
            .YINNI:"Group 13123168751"
        ],"Group13123168729":[
            .YINDU:"Group13123168729",
            .YINNI:"fewfewfewf33333333"
        ],"Groupfew1312316878":[
            .YINDU:"Groupfew1312316878",
            .YINNI:"Group1fewfwefw312316879"
        ],"Group 117127733622222222":[
            .YINDU:"Group 117127733622222222",
            .YINNI:"Group 117127733611111111"
        ],"Group 1171277337dddddd":[
            .YINDU:"Group 1171277337dddddd",
            .YINNI:"Group 1171277337ffffff"
        ],"Group 1171277332":[
            .YINDU:"Group 1171277332",
            .YINNI:"Group 1171277332fewefwefwe"
        ],"Group 1171277333eeeeeeee":[
            .YINDU:"Group 1171277333eeeeeeee",
            .YINNI:"Group 117127733fewfwfwfwe3"
        ],"Group 1312316870eeeeee22222":[
            .YINDU:"Group 1312316870eeeeee22222",
            .YINNI:"Group 13123168ddddddddd74"
        ],"Group 1171dfewf2f32f23f23f2f23276890":[
            .YINDU:"Group 1171dfewf2f32f23f23f2f23276890",
            .YINNI:"Group 11712768ddf33232323290"
        ],"Group 1171276874aaaaaa":[
            .YINDU:"Group 1171276874aaaaaa",
            .YINNI:"Group 1171276874bbbbbb"
        ],"Group 1171277340":[
            .YINDU:"Group 1171277340",
            .YINNI:"Group 1171277339"
        ],"hrt4554454":[
            .YINDU:"hrt4554454",
            .YINNI:"fwe2332223"
        ],"Groupfwefwfwe3331171277331":[
            .YINDU:"Groupfwefwfwe3331171277331",
            .YINNI:"Groupfwew3333fe1171277331"
        ]
        
        
    ]
    
    // MARK: - Private
    fileprivate let localizedStrings: [String: [AppLanguage: String]] = [
        "Mobile number":[
            .YINDU:"Mobile number",
            .YINNI:"Nomor telepon seluler"
        ],
        "Verification code":[
            .YINDU:"Verification code",
            .YINNI:"Kode verifikasi"
        ],
        "Voice verifcation?":[
            .YINDU:"Voice verifcation?",
            .YINNI:"Verifikasi suara?"
        ],
        "Login":[
            .YINDU:"Login",
            .YINNI:"Masuk"
        ],
        "I have read and agree":[
            .YINDU:"I have read and agree",
            .YINNI:"Saya telah membaca \ndan menyetujui"
        ],
        "<Privacy Agreement>":[
            .YINDU:"<Privacy Agreement>",
            .YINNI:"<Perjanjian Privasi>"
        ],"<Loan Agreement>":[
            .YINDU:"<Loan Agreement>",
            .YINNI:"<Perjanjian Pinjaman>"
        ],
        "Enter mobile number":[
            .YINDU:"Enter mobile number",
            .YINNI:"Masukkan nomor telepon seluler"
        ],
        "Get Code":[
            .YINDU:"Get Code",
            .YINNI:"Dapatkan Kode"
        ],
        "Other services":[
            .YINDU:"Other services",
            .YINNI:"Layanan lain"
        ],
        "Start":[
            .YINDU:"Start",
            .YINNI:"Mulai"
        ],
        "Next":[
            .YINDU:"Next",
            .YINNI:"Selanjutnya"
        ],"Confirm":[
            .YINDU:"Confirm",
            .YINNI:"Konfirmasi"
        ],"Please confirm":[
            .YINDU:"Please confirm",
            .YINNI:"Silakan konfirmasi"
        ],"Please check your lD information correctly, once submitted it is not changed again":[
            .YINDU:"Please check your lD information correctly, once submitted it is not changed again",
            .YINNI:"Silakan periksa informasi lD Anda dengan benar, setelah dikirimkan, informasi tersebut tidak dapat diubah lagi."
        ],"Name":[
            .YINDU:"Name",
            .YINNI:"Nama."
        ],"ID number":[
            .YINDU:"ID number",
            .YINNI:"Nomor identitas"
        ],"Birthday":[
            .YINDU:"Birthday",
            .YINNI:"Ulang Tahun"
        ],"Confirm card number":[
            .YINDU:"Confirm card number",
            .YINNI:"Konfirmasi nomor kartu"
        ],"Please confirm whether the information is correct?":[
            .YINDU:"Please confirm whether the information is correct?",
            .YINNI:"Silakan konfirmasikan apakah informasi itu benar?"
        ],"Date Selection":[
            .YINDU:"Date Selection",
            .YINNI:"Pemilihan Tanggal"
        ],
        "Day":[
            .YINDU:"Day",
            .YINNI:"Hari"
        ],
        "Month":[
            .YINDU:"Month",
            .YINNI:"Bulan"
        ],
        "Years":[
            .YINDU:"Years",
            .YINNI:"Tahun"
        ],"Please select an address":[
            .YINDU:"Please select an address",
            .YINNI:"Silakan pilih alamat"
        ],"Our users say":[
            .YINDU:"Our users say",
            .YINNI:"Pengguna kami mengatakan"
        ],"Product data display":[
            .YINDU:"Product data display",
            .YINNI:"Tampilan data produk"
        ],"Popular recommendations":[
            .YINDU:"Popular recommendations",
            .YINNI:"Rekomendasi populer"
        ],"I have read and agreed to the above":[
            .YINDU:"I have read and agreed to the above",
            .YINNI:"Saya telah membaca dan menyetujui ketentuan di atas."
        ],"Hello! Welcome to Pinjaman Instan!":[
            .YINDU:"Hello! Welcome to Pinjaman Instan!",
            .YINNI:"Halo! Selamat datang di Pinjaman Instan!"
        ],"My functions":[
            .YINDU:"My functions",
            .YINNI:"Fungsi-fungsi saya"
        ],"Please enter your mobile number":[
            .YINDU:"Please enter your mobile number",
            .YINNI:"Silakan masukkan nomor ponsel Anda"
        ],"Please enter the verification code":[
            .YINDU:"Please enter the verification code",
            .YINNI:"Masukkan kode verifikasi"
        ],"Please select the agreement":[
            .YINDU:"Please select the agreement",
            .YINNI:"Silakan pilih perjanjian"
        ],"My":[
            .YINDU:"My",
            .YINNI:"Saya"
        ],"Settings":[
            .YINDU:"Settings",
            .YINNI:"Pengaturan"
        ],"Order Center":[
            .YINDU:"Order Center",
            .YINNI:"Pusat Pesanan"
        ],"All":[
            .YINDU:"All",
            .YINNI:"Semua"
        ],"Apply":[
            .YINDU:"Apply",
            .YINNI:"Terapkan"
        ],"Repayment":[
            .YINDU:"Repayment",
            .YINNI:"Pembayaran"
        ],"Finished":[
            .YINDU:"Finished",
            .YINNI:"Selesai"
        ],"Go to Apply":[
            .YINDU:"Go to Apply",
            .YINNI:"Pergi ke Apply"
        ],"no order yet":[
            .YINDU:"no order yet",
            .YINNI:"belum ada pesanan"
        ],"view>":[
            .YINDU:"view>",
            .YINNI:"tampilan>"
        ],"Certification conditions":[
            .YINDU:"Certification conditions",
            .YINNI:"Sertifikasi kondisi"
        ],"Permission":[
            .YINDU:"Permission",
            .YINNI:"Sertifikasi kondisi"
        ],"Camera permission is turned off. To continue using these features, please open Settings > Privacy > Camera, find our app, and enable camera access.":[
            .YINDU:"Camera permission is turned off. To continue using these features, please open Settings > Privacy > Camera, find our app, and enable camera access.",
            .YINNI:"Izin kamera dimatikan. Untuk terus menggunakan fitur ini, silakan buka Pengaturan > Privasi > Kamera, cari aplikasi kami, dan aktifkan akses kamera."
        ],"Cancel":[
            .YINDU:"Cancel",
            .YINNI:"Batal"
        ],"Please read and agree to the Privacy Agreement":[
            .YINDU:"Please read and agree to the Privacy Agreement",
            .YINNI:"Harap baca dan setujui Perjanjian Privasi"
        ],
        "Please choose":[
            .YINDU:"Please choose",
            .YINNI:"Silakan pilih"
        ],"Please Selecte":[
            .YINDU:"Please Selecte",
            .YINNI:"Silakan Pilih"
        ],"Account cancellation":[
            .YINDU:"Account cancellation",
            .YINNI:"Pembatalan akun"
        ],"Sign out":[
            .YINDU:"Sign out",
            .YINNI:"Keluar"
        ],"Contacts Permission":[
            .YINDU:"Contacts Permission",
            .YINNI:"Izin Kontak"
        ],"Contacts access is denied. To enable inviting and sharing functions, please open Settings > Privacy > Contacts, locate our app, and allow contact access.":[
            .YINDU:"Contacts access is denied. To enable inviting and sharing functions, please open Settings > Privacy > Contacts, locate our app, and allow contact access.",
            .YINNI:"Akses kontak ditolak. Untuk mengaktifkan fungsi undangan dan berbagi, silakan buka Pengaturan > Privasi > Kontak, cari aplikasi kami, dan izinkan akses kontak."
        ],"You'll have your loan in no time! Sure you want out?":[
            .YINDU:"You'll have your loan in no time! Sure you want out?",
            .YINNI:"Pinjaman Anda akan segera cair! Yakin ingin keluar?"
        ],"Hello! Welcome To Pinjaman Instan!":[
            .YINDU:"Hello! Welcome To Pinjaman Instan!",
            .YINNI:"Halo! Selamat datang di Pinjaman Instan!"
        ],
        
    ]
}



